package com.example.server8080.server;

import com.example.server8080.service.ArithService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.grpc.Server;

@Component
public class GrpcServer {
    //~ Static fields/constants/initializer

    private static final Logger log = LoggerFactory.getLogger(GrpcServer.class);

    private Server server;
    @Value("${rpc.port}")
    private int rpcPort;

    public void start() throws Exception {
        this.server = io.grpc.ServerBuilder.forPort(rpcPort).addService(new ArithService()).build().start();

        // 添加关闭服务器的回调，将server(socket)关闭掉...
        Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
        log.info("rpc server started on port: {}", rpcPort);

        this.server.awaitTermination();
    }

    public void stop() {
        if (this.server != null) {
            log.info("rpc server shutdown...");
            this.server.shutdown();
        }
    }

}
